package online.sh.cn.fy.users.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import online.sh.cn.fy.common.utils.CookieUtil;
import online.sh.cn.fy.common.utils.DES3Util;
import online.sh.cn.fy.common.utils.TokenUtils;
import online.sh.cn.fy.common.utils.WxUtils;

/**
 * 签到日历
 */
@WebServlet("/signeveryday")
public class SignUpEveryDayServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String code=request.getParameter("code");
		WxUtils wx=new WxUtils();
		if (null!=code && code!="") {
			JSONObject jsonopenid=JSONObject.fromObject(wx.getOpenidAndToken(code));
			String openid=jsonopenid.getString("openid");
			TokenUtils to=new TokenUtils();
			String check=to.getUserMobile(openid);
			try {
				openid=DES3Util.encrypt(openid);
			} catch (Exception e) {
				log(e.getMessage());
			}
//			openid=CookieUtil.encodeCookie(openid);
			CookieUtil.saveCookie(response, "openid", openid);
			if(null!=check && ""!=check){
				CookieUtil.saveCookie(response, "uuid", "0");     //0代表young粉用户
			}else{
				CookieUtil.saveCookie(response, "uuid", "1");	  //1代表非young粉用户 
			}
			response.sendRedirect("calendar.html");
		}else{
			response.sendRedirect("error.html");
		}
	}

}
